package king.dominic.origin;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.googlecode.leptonica.android.Convert;
import com.googlecode.leptonica.android.Enhance;
import com.googlecode.leptonica.android.GrayQuant;
import com.googlecode.leptonica.android.MorphApp;
import com.googlecode.leptonica.android.Pix;
import com.googlecode.leptonica.android.ReadFile;
import com.googlecode.leptonica.android.WriteFile;

import java.io.File;

import king.dominic.jlibrary.activity.OriginActivity;
import king.dominic.jlibrary.util.Camera;
import king.dominic.jlibrary.util.ImageUtil;
import king.dominic.jlibrary.util.PermissionUtil;
import king.dominic.jlibrary.view.WindowUtil;
import king.dominic.jorc.ui.ScanCardActivity;

public class MainActivity extends OriginActivity {

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    private ImageView imageView;
    private String mPath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EditText
        setContentView(R.layout.activity_main);
        // Example of a call to a native method
        TextView tv = (TextView) findViewById(R.id.sample_text);
        tv.setText(stringFromJNI());
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ScanCardActivity.startForResult(MainActivity.this, true,101);
            }
        });
        View textView = findViewById(R.id.textView);
        imageView = findViewById(R.id.imageView);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PermissionUtil.callMethod(MainActivity.this, new String[]{
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE}, new Runnable() {
                    @Override
                    public void run() {
                        if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ||
                                ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                            return;
                        }
                        Camera.getImageFromAlbum(MainActivity.this);
                    }
                });
            }
        });
        findViewById(R.id.button2).setOnClickListener(listener);
        findViewById(R.id.button3).setOnClickListener(listener);
        findViewById(R.id.button4).setOnClickListener(listener);
        findViewById(R.id.button5).setOnClickListener(listener);
    }

    private View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Pix p = ReadFile.readFile(new File(mPath));
            Pix pix = Convert.convertTo8(p);
            p.recycle();
            switch (v.getId()) {
                case R.id.button2:
                    Pix pix5 = MorphApp.pixFastTophat(pix,12,12,1);
                    pix.recycle();
                    Pix pix6 = GrayQuant.pixThresholdToBinary(pix5, 62);
                    pix5.recycle();
                    setBitmap(pix6);
                    break;
                case R.id.button3:
                    Pix pix2 = Enhance.unsharpMasking(pix,5,0.5f);
                    pix.recycle();
                    setBitmap(pix2);
                    break;
                case R.id.button4:
                    Pix pix3 = GrayQuant.pixThresholdToBinary(pix, 62);
                    pix.recycle();
                    setBitmap(pix3);
                    break;
                case R.id.button5:
                    Pix pix4 = MorphApp.pixFastTophat(pix,12,12,1);
                    pix.recycle();
                    setBitmap(pix4);
                    break;
            }
        }
    };

    private void setBitmap(Pix pix) {
        Bitmap bitmap = WriteFile.writeBitmap(pix);
        pix.recycle();
        if (bitmap != null)
            imageView.setImageBitmap(bitmap);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==101 && resultCode == Activity.RESULT_OK && data!=null){
            String image = ScanCardActivity.obtainCardImage(data);
            ImageUtil.showImage(imageView, image);
            Log.d("...", "onActivityResult: "+ScanCardActivity.obtainCard(data));
            return;
        }
        Camera.onActivityResult(this, requestCode, resultCode, data, new Camera.OnActivityResultListener() {
            @Override
            public void requestCaptureCamera(String path) {
                mPath = path.replaceAll("file://", "");
                ImageUtil.showImage(imageView, path);
            }

            @Override
            public void requestCrop(String path) {

            }
        });
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();
}
